﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectStudy.App_Code;
using System.Data.SqlClient;
using System.Data;

namespace ProjectStudy
{
    public partial class MainFrame :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["username"] != null)
                {
                    Label1.Text = "欢迎，"+Session["username"];
                    //Label2.Text = Request.ServerVariables["HTTP_REFERER"] + "," + Request.ServerVariables["SERVER_NAME"];
                    tree.ShowLines = true;
                    tree.ShowExpandCollapse = true;
                    TreeNodeCollection tnc = new TreeNodeCollection();
                    tnc = tree.Nodes;
                    //开始调用tncXunHuan，数据库中fuid=9999为根节点  
                    tncXunHuan(tnc, -1);     
                }
            }
        }

        //树形菜单  
        public void tncXunHuan(TreeNodeCollection tnc, int fuid)
        {
            try
            {
                string sql = "select menuID,FatherId,menuName,Hyperlink from Menu where FatherId=@faId and groupId=@group";
                SqlParameter[] ps = { 
                                    new SqlParameter("@faId", fuid),
                                    new SqlParameter("@group",Session["group"].ToString())
                                    }; 
                DataSet ds = DBAccess.ExecuteDataset(sql,ps);

                // ----------------end------------数据库获取数据  
                //------------------start-----------根据数据结构向treeView添加节点  
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int id = Convert.ToInt16(dr["menuID"]);
                    int Faid = Convert.ToInt16(dr["FatherId"]);
                    string menuName = dr["menuName"].ToString();
                    TreeNode treenode = new TreeNode();
                    treenode.Text = menuName;
                    treenode.NavigateUrl = dr["Hyperlink"].ConvertObjToStr();
                    //tn.ImageUrl = "images/file.png";//默认图标为file.png                     
                    tnc.Add(treenode);
                    int tncInt = ds.Tables[0].Rows.IndexOf(dr);
                    tncXunHuan(tnc[tncInt].ChildNodes, id); //----------递归调用  
                    // tnc = tnc;  
                }
                //------------------end-----------根据数据结构向treeView添加节点  
                //------------------start---------设置包含子节点的父节点的图标  
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //tnc[0].Parent.ImageUrl = "images/openfoldericon.png";//设置父文件图标  
                }
                else
                {
                    //tnc[0].Parent.ImageUrl = "images/file.png";
                }
            }
            catch (Exception e) { return; }
        }  

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = null;
            Response.Redirect("登录界面.aspx");
        }
    }
}